import types from '../../store/action_types'

export default {
  // 状态类型
  TYPES: types,
  // 提交状态信息
  commit: function (type, payload) {
    this.$store.dispatch({
      type: type,
      payload: payload
    })
  },
  // 显示消息
  showMsg: function (options) {
    let config = {
      type: 'none'
    }
    if (this.isObject(options)) {
      config = Object.assign({}, config, options)
    }
    if (typeof options === 'string') {
      config = Object.assign({}, config, {
        text: options
      })
    }
    // 关闭
    if (typeof options === 'boolean') {
      config = Object.assign({}, config, {
        close: options
      })
    }

    let text = config.text || 'none'
    let type = config.type || 'none'
    let close = config.close || false
    let time = config.time || 3000

    if (type === 'loading') {
      time = 999999
    }
    if (text === '' && type === 'none') {
      return
    }

    // 关闭
    if (close) {
      this.$api.hideToast()
      return
    }

    this.$api.showToast({
      icon: type,
      title: text,
      duration: time
    })
  },
  // 是否对象
  isObject: function (obj) {
    let type = Object.prototype.toString.call(obj)

    return type === '[object Object]'
  }
}
